package com.shuangfeidasan.controller.ums;

import com.shuangfeidasan.api.CommonResult;
import com.shuangfeidasan.dto.AdminInfoParam;
import com.shuangfeidasan.dto.AdminLoginParam;
import com.shuangfeidasan.model.ums.UmsAdmin;
import com.shuangfeidasan.service.ums.impl.AdminSecurityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author JunWEI
 * @Date 2022/1/23 下午4:04
 */
@Api(tags = "UmsAdminAuthController", description = "权限认证相关api")
@RestController
@RequestMapping("auth")
public class UmsAdminAuthController {
    @Autowired
    private AdminSecurityService adminSecurityService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    @ResponseBody
    public CommonResult login(@Validated @RequestBody AdminLoginParam adminLoginParam) {
        String token = adminSecurityService.login(adminLoginParam);
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation(value = "用户注册")
    @PostMapping(value = "/register")
    @ResponseBody
    public CommonResult<UmsAdmin> register(@Validated @RequestBody UmsAdmin UmsAdmin) {
        UmsAdmin umsAdmin = adminSecurityService.register(UmsAdmin);
        if (umsAdmin == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(umsAdmin);
    }

    @ApiOperation(value = "更新用户")
    @PostMapping(value = "/update")
    @ResponseBody
    public CommonResult<UmsAdmin> update(@Validated @RequestBody UmsAdmin UmsAdmin) {
        UmsAdmin umsAdmin = adminSecurityService.update(UmsAdmin);
        if (umsAdmin == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(umsAdmin);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping(value = "/info")
    @ResponseBody
    public CommonResult getAdminInfo(Principal principal) {
        if (principal == null) {
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        AdminInfoParam adminInfoParam = adminSecurityService.getUserInfo(username);
//        UmsAdmin umsAdmin = adminService.getAdminByUsername(username);
//        Map<String, Object> data = new HashMap<>();
//        data.put("username", umsAdmin.getUsername());
//        data.put("menus", roleService.getMenuList(umsAdmin.getId()));
//        data.put("icon", umsAdmin.getIcon());
//        List<UmsRole> roleList = adminService.getRoleList(umsAdmin.getId());
//        if(CollUtil.isNotEmpty(roleList)){
//            List<String> roles = roleList.stream().map(UmsRole::getName).collect(Collectors.toList());
//            data.put("roles",roles);
//        }
        return CommonResult.success(adminInfoParam);
    }
}
